package com.san.oms.controller;

import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.san.base.controller.BaseController;
import com.san.common.annotation.Log;
import com.san.common.utils.PageUtils;
import com.san.common.utils.Query;
import com.san.common.utils.R;
import com.san.oms.domain.ScoreLevelDetailDO;
import com.san.oms.service.ScoreLevelDetailService;
import com.san.oms.vo.ScoreLevelDetailVO;

@RequestMapping("/ce/scoreleveldetail")
@Controller
public class ScoreLevelDetailController extends BaseController{

	private String PREFIX = "ce/scoreleveldetail";
	private static final Logger logger = LoggerFactory.getLogger(ScoreLevelDetailController.class);
	@Autowired
	private ScoreLevelDetailService scoreLevelDetailService;
	
	@GetMapping("/view/{scoreId}")
	@RequiresPermissions("ce:scoreleveldetail:view")
	String source(Model model, @PathVariable("scoreId") Long scoreId) {
		model.addAttribute(scoreId);
		return PREFIX + "/view";
	}
	
	/**
	 * 查询评分等级明细列表
	 * @param params
	 * @return
	 */
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("ce:scoreleveldetail:view")
	public PageUtils list(@RequestParam Map<String, Object> params){
		PageUtils pageUtils = null;
		List<ScoreLevelDetailDO> scoreLevelDetailList = null;
		try{
			// 查询列表数据
			Query query = new Query(params);
			scoreLevelDetailList = scoreLevelDetailService.list(query);
			int total = scoreLevelDetailService.count(query);
			pageUtils = new PageUtils(scoreLevelDetailList, total);
		}catch(Exception e){
			logger.error("获取等级明细列表失败",e);
		}
		return pageUtils;
	}
	
	/**
	 * 进入新增页面
	 * @return
	 */
	@GetMapping("/add/{scoreId}")
	@RequiresPermissions("ce:scoreleveldetail:add")
	String add(Model model,@PathVariable("scoreId") Long scoreId) {
		model.addAttribute(scoreId);
		return PREFIX + "/add";
	}
	
	/**
	 * 保存新增评分等级明细
	 * @param code
	 * @return
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("ce:scoreleveldetail:add")
	@Log("新增评分等级明细")
	public R save(ScoreLevelDetailVO scoreLevelDetail) {
		try{
				int result = scoreLevelDetailService.save(scoreLevelDetail);
				if(result == -1){
					return R.error("该等级明细已经存在");
				}if(result == -2){
					return R.error("分数区间上限应高于下限");
				}else if (result > 0) {
					return R.ok();
				}
			}catch (Exception e) {
				logger.error("新增等级明细失败",e);
			}
		return R.error();
	}
	
	/**
	 * 编辑评分等级明细
	 * @param id
	 * @param model
	 * @return
	 */
	@GetMapping("/edit/{id}")
	@RequiresPermissions("ce:scoreleveldetail:edit")
	@Log("编辑评分等级明细")
	String edit(@PathVariable("id") Long id, Model model) {
		ScoreLevelDetailDO ScoreLevelDetailDO = scoreLevelDetailService.getById(id);
		model.addAttribute("ScoreLevelDetailDO", ScoreLevelDetailDO);
		return PREFIX + "/edit";
	}
	
	/**
	 * 修改评分等级明细
	 * @param code
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/doEdit")
	@RequiresPermissions("ce:scoreleveldetail:edit")
	@Log("修改评分等级")
	public R doEdit(ScoreLevelDetailVO scoreLevelDetail) {
		try{
			int result = scoreLevelDetailService.doEdit(scoreLevelDetail);
			if(result == -1){
				return R.error("该等级明细已经存在");
			}if(result == -2){
				return R.error("分数区间上限应高于下限");
			}else if(result > 0){
				return R.ok();
			}
		}catch(Exception e){
			logger.error("修改等级明细失败",e);
		}
		return R.error();
	}
	
	/**
	 * 删除评分等级明细
	 * @param id
	 * @return
	 */
	@PostMapping("/remove")
	@ResponseBody
	@RequiresPermissions("ce:scoreleveldetail:remove")
	@Log("删除评分等级明细")
	public R remove(Long id) {
		if (scoreLevelDetailService.remove(id) > 0) {
			return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 完全删除评分等级明细
	 * @param id
	 * @return
	 */
	@PostMapping("/remove1")
	@ResponseBody
	@RequiresPermissions("ce:scoreleveldetail:remove1")
	@Log("完全删除评分等级明细")
	public R remove1(Long id) {
		if (scoreLevelDetailService.remove1(id) > 0) {
			return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 批量删除评分等级
	 */
	@PostMapping("/batchRemove")
	@ResponseBody
	@RequiresPermissions("ce:scoreleveldetail:batchRemove")
	@Log("批量删除评分等级明细")
	public R remove(@RequestParam("ids[]") Long[] ids) {
		scoreLevelDetailService.batchRemove(ids);
		return R.ok();
	}
}
